package headfirst.combined.djview;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.io.File;
import java.awt.Panel;
import java.awt.Font;

import javax.swing.JToggleButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MusicMixPad implements ActionListener,  BeatObserver, BPMObserver{

	private static JFrame frame;
	private static JButton botonP,botonB,botonR;
	private JButton Do,Re,Mi,Fa,Sol,La,Si,DoS,Mib,FaS,SolS,Sib;
	private BeatBar beatBar1, beatBar2, beatBar3;
	private JButton btnStart, btnStop, next, prev, Set;
	private JToggleButton botonManual;
	private TransparentPanel panel;
	private JLabel text;
	/*public static void main(String []args){
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MusicMixPad window = new MusicMixPad(null,null);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/
	private ControllerInterface controller;
	private BeatModelInterface model;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JTextField textField;
	private JButton button;
	private JButton button_1;
	
	public MusicMixPad (ControllerInterface controller, BeatModelInterface model){
		this.controller = controller;
		this.model = model;

		model.registerObserver((BeatObserver)this);
		model.registerObserver((BPMObserver)this);
		//this.frame.setVisible(true);
		//MusicMixPad window = new MusicMixPad(null,null);
		//window.frame.setVisible(true);
		initialize();
	}
			
	private void initialize() {		
		  // Create and set up the window.
		frame = new JFrame("DJ TEST DRIVE MIX PAD CENTER");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		panel = new TransparentPanel();
		panel.setBackgroundImage(new ImageIcon("fondo2.jpg").getImage());
		  
		Component contents = panel;
		frame.getContentPane().add(contents, BorderLayout.CENTER);
		panel.setLayout(null);
		 
		  /*
		  botonManual = new JButton("Modo Pad");
		  panel.add(botonManual);
			botonManual.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent tecla) {
					if(tecla.VK_A==tecla.getKeyCode()){
						botonB.setBackground(Color.RED);
						playBombo();
					}
					if(tecla.VK_S==tecla.getKeyCode()){
						botonP.setBackground(Color.BLUE);
						playPlatillo();
					}
					if(tecla.VK_D==tecla.getKeyCode()){
						botonR.setBackground(Color.GREEN);
						playRedoblante();
					}
				}
				@Override
				public void keyReleased(KeyEvent tecla) {
					if(tecla.VK_A==tecla.getKeyCode()){
						botonB.setBackground(null);
					}
					if(tecla.VK_S==tecla.getKeyCode()){
						botonP.setBackground(null);
					}
					if(tecla.VK_D==tecla.getKeyCode()){
						botonR.setBackground(null);
					}
				}
			});
			botonManual.setBounds(20, 155, 120, 54);
			*/

			//////////////////////////////////////////////////////////////////////////////
			//Botones y Acciones
			botonB = new JButton("Bombo");
			botonB.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//Crear instrumento
					//Reproducir mientras queda apretado
					//Stopear al apagar
					playBombo();
				}
			});
			
			textField = new JTextField();
			textField.setHorizontalAlignment(SwingConstants.CENTER);
			textField.setBounds(67, 219, 113, 32);
			panel.add(textField);
			textField.setColumns(10);
			
			JLabel lblNewLabel = new JLabel("Roland SPD-SX Sampling Pad");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setForeground(Color.LIGHT_GRAY);
			lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 35));
			lblNewLabel.setBounds(212, 11, 462, 83);
			panel.add(lblNewLabel);
			panel.add(botonB);
			botonB.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					
				}
			});
			botonB.setBounds(250, 262, 89, 54);
			
			
			
			
			botonP = new JButton("Platillo");
			botonP.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			panel.add(botonP);
			botonP.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					playPlatillo();
				}
			});
			botonP.setBounds(349, 262, 89, 54);
			
			
			botonR = new JButton("Redoblante");
			panel.add(botonR);
			botonR.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					playRedoblante();
				}
			});
			botonR.setBounds(448, 262, 87, 53);
			
			Do = new JButton("DO");
			Do.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Do.setBackground(Color.WHITE);
			Do.setBounds(105, 369, 48, 107);
			panel.add(Do);
			Do.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent tecla) {
					if(tecla.VK_Z==tecla.getKeyCode()){
						//botonB.setBackground(Color.RED);
						//Boton Z presionado
					}
				}});
			Re = new JButton("RE");
			Re.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Re.setBackground(Color.WHITE);
			Re.setBounds(178, 369, 47, 107);
			panel.add(Re);
			
			Mi = new JButton("MI");
			Mi.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Mi.setBackground(Color.WHITE);
			Mi.setBounds(250, 369, 47, 107);
			panel.add(Mi);
			
			Fa = new JButton("FA");
			Fa.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Fa.setBackground(Color.WHITE);
			Fa.setBounds(296, 369, 47, 107);
			panel.add(Fa);
			
			Sol = new JButton("SOL");
			Sol.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Sol.setBackground(Color.WHITE);
			Sol.setBounds(368, 369, 47, 107);
			panel.add(Sol);
			
			La = new JButton("LA");
			La.setFont(new Font("Tahoma", Font.PLAIN, 8));
			La.setBackground(Color.WHITE);
			La.setBounds(440, 369, 47, 107);
			panel.add(La);
			
			FaS = new JButton("");
			FaS.setBackground(Color.BLACK);
			FaS.setBounds(342, 369, 27, 51);
			panel.add(FaS);
			
			DoS = new JButton("");
			DoS.setBackground(Color.BLACK);
			DoS.setBounds(152, 369, 27, 51);
			panel.add(DoS);
			
			Mib = new JButton("");
			Mib.setBackground(Color.BLACK);
			Mib.setBounds(224, 369, 27, 51);
			panel.add(Mib);
			
			SolS = new JButton("");
			SolS.setBackground(Color.BLACK);
			SolS.setBounds(414, 369, 27, 51);
			panel.add(SolS);
			
			Si = new JButton("SI");
			Si.setFont(new Font("Tahoma", Font.PLAIN, 8));
			Si.setBackground(Color.WHITE);
			Si.setBounds(512, 369, 47, 107);
			panel.add(Si);
			
			Sib = new JButton("");
			Sib.setBackground(Color.BLACK);
			Sib.setBounds(486, 369, 27, 51);
			panel.add(Sib);
			 
			 
			  
			  btnStart = new JButton("Play");
			  btnStart.addActionListener(new ActionListener() {
			  	public void actionPerformed(ActionEvent arg0) {
			  		controller.start();
			  	}
			  });
			  btnStart.setBounds(121, 11, 89, 31);
			  panel.add(btnStart);
			  
			  btnStop = new JButton("Stop");
			  btnStop.addActionListener(new ActionListener() {
			  	public void actionPerformed(ActionEvent arg0) {
			  		controller.stop();
			  	}
			  });
			  btnStop.setBounds(121, 47, 89, 32);
			  panel.add(btnStop);
			  
			  
			  //BARRA DE PROGRESO-------------------------------------------------------
			    beatBar1 = new BeatBar();
			    beatBar1.setBounds(250, 228, 89, 23);
				panel.add(beatBar1);
				
				beatBar2 = new BeatBar();
				beatBar2.setBounds(349, 228, 89, 23);
				panel.add(beatBar2);
				
				beatBar3 = new BeatBar();
				beatBar3.setBounds(446, 228, 89, 23);
				panel.add(beatBar3);
			  
			  
			  //BOTON MANUAL------------------------------------------------------------
			  botonManual = new JToggleButton("Pad Mode");
			  //botonManual = new JButton("Modo Pad");
			  //panel.add(botonManual);
			  botonManual.addKeyListener(new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent tecla) {
						if(tecla.VK_I==tecla.getKeyCode()){
							botonB.setBackground(Color.RED);
							playBombo();
						}
						if(tecla.VK_O==tecla.getKeyCode()){
							botonP.setBackground(Color.BLUE);
							playPlatillo();
						}
						if(tecla.VK_P==tecla.getKeyCode()){
							botonR.setBackground(Color.GREEN);
							playRedoblante();
						}
						if(tecla.VK_Z==tecla.getKeyCode()){
							Do.setBackground(Color.GRAY);
							//Nota DO
						}
						if(tecla.VK_X==tecla.getKeyCode()){
							Re.setBackground(Color.GRAY);
							//Nota RE
						}
						if(tecla.VK_C==tecla.getKeyCode()){
							Mi.setBackground(Color.GRAY);
							//Nota MI
						}
						if(tecla.VK_V==tecla.getKeyCode()){
							Fa.setBackground(Color.GRAY);
							//Nota FA
						}
						if(tecla.VK_B==tecla.getKeyCode()){
							Sol.setBackground(Color.GRAY);
							//Nota SOL
						}
						if(tecla.VK_N==tecla.getKeyCode()){
							La.setBackground(Color.GRAY);
							//Nota LA
						}
						if(tecla.VK_M==tecla.getKeyCode()){
							Si.setBackground(Color.GRAY);
							//Nota SI
						}
						if(tecla.VK_S==tecla.getKeyCode()){
							DoS.setBackground(Color.GRAY);
							//Nota DO#
						}
						if(tecla.VK_D==tecla.getKeyCode()){
							Mib.setBackground(Color.GRAY);
							//Nota MIb
						}
						if(tecla.VK_G==tecla.getKeyCode()){
							FaS.setBackground(Color.GRAY);
							//Nota FA#
						}
						if(tecla.VK_H==tecla.getKeyCode()){
							SolS.setBackground(Color.GRAY);
							//Nota SOL#
						}
						if(tecla.VK_J==tecla.getKeyCode()){
							Sib.setBackground(Color.GRAY);
							//Nota SIb
						}
					}
					@Override
					public void keyReleased(KeyEvent tecla) {
						if(tecla.VK_I==tecla.getKeyCode()){
							botonB.setBackground(null);
						}
						if(tecla.VK_O==tecla.getKeyCode()){
							botonP.setBackground(null);
						}
						if(tecla.VK_P==tecla.getKeyCode()){
							botonR.setBackground(null);
						}
						if(tecla.VK_Z==tecla.getKeyCode()){
							Do.setBackground(Color.WHITE);
							//Nota DO
						}
						if(tecla.VK_X==tecla.getKeyCode()){
							Re.setBackground(Color.WHITE);
							//Nota RE
						}
						if(tecla.VK_C==tecla.getKeyCode()){
							Mi.setBackground(Color.WHITE);
							//Nota MI
						}
						if(tecla.VK_V==tecla.getKeyCode()){
							Fa.setBackground(Color.WHITE);
							//Nota FA
						}
						if(tecla.VK_B==tecla.getKeyCode()){
							Sol.setBackground(Color.WHITE);
							//Nota SOL
						}
						if(tecla.VK_N==tecla.getKeyCode()){
							La.setBackground(Color.WHITE);
							//Nota LA
						}
						if(tecla.VK_M==tecla.getKeyCode()){
							Si.setBackground(Color.WHITE);
							//Nota SI
						}
						if(tecla.VK_S==tecla.getKeyCode()){
							DoS.setBackground(Color.BLACK);
							//Nota DO#
						}
						if(tecla.VK_D==tecla.getKeyCode()){
							Mib.setBackground(Color.BLACK);
							//Nota MIb
						}
						if(tecla.VK_G==tecla.getKeyCode()){
							FaS.setBackground(Color.BLACK);
							//Nota FA#
						}
						if(tecla.VK_H==tecla.getKeyCode()){
							SolS.setBackground(Color.BLACK);
							//Nota SOL#
						}
						if(tecla.VK_J==tecla.getKeyCode()){
							Sib.setBackground(Color.BLACK);
							//Nota SIb
						}
					}
				});
				///botonManual.setBounds(20, 155, 120, 54);
				botonManual.setBounds(10, 11, 98, 68);
				panel.add(botonManual);
				
				JLabel lblBpm = new JLabel("BPM");
				lblBpm.setForeground(Color.GREEN);
				lblBpm.setHorizontalAlignment(SwingConstants.CENTER);
				lblBpm.setFont(new Font("Ebrima", Font.BOLD, 24));
				lblBpm.setBounds(250, 185, 89, 32);
				panel.add(lblBpm);
				
				label = new JLabel("BPM");
				label.setForeground(Color.GREEN);
				label.setHorizontalAlignment(SwingConstants.CENTER);
				label.setFont(new Font("Ebrima", Font.BOLD, 24));
				label.setBounds(349, 185, 89, 32);
				panel.add(label);
				
				label_1 = new JLabel("BPM");
				label_1.setForeground(Color.GREEN);
				label_1.setHorizontalAlignment(SwingConstants.CENTER);
				label_1.setFont(new Font("Ebrima", Font.BOLD, 24));
				label_1.setBounds(446, 185, 89, 32);
				panel.add(label_1);
				
				label_2 = new JLabel("240");
				label_2.setForeground(Color.GREEN);
				label_2.setHorizontalAlignment(SwingConstants.CENTER);
				label_2.setFont(new Font("Ebrima", Font.BOLD, 24));
				label_2.setBounds(250, 155, 89, 32);
				panel.add(label_2);
				
				label_3 = new JLabel("240");
				label_3.setForeground(Color.GREEN);
				label_3.setHorizontalAlignment(SwingConstants.CENTER);
				label_3.setFont(new Font("Ebrima", Font.BOLD, 24));
				label_3.setBounds(349, 155, 89, 32);
				panel.add(label_3);
				
				label_4 = new JLabel("240");
				label_4.setForeground(Color.GREEN);
				label_4.setHorizontalAlignment(SwingConstants.CENTER);
				label_4.setFont(new Font("Ebrima", Font.BOLD, 24));
				label_4.setBounds(446, 155, 89, 32);
				panel.add(label_4);
				
				text = new JLabel("Instrumento: Bombo BPM: 90");
				text.setForeground(Color.GREEN);
				text.setFont(new Font("Tahoma", Font.PLAIN, 13));
				text.setHorizontalAlignment(SwingConstants.CENTER);
				text.setBounds(10, 302, 236, 14);
				panel.add(text);
				
				Set = new JButton("SET");
				Set.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						int bpm = Integer.parseInt(textField.getText());
			        	controller.setBPM(bpm);
					}
				});
				Set.setBounds(67, 255, 113, 42);
				panel.add(Set);
				
				next = new JButton(">>");
				next.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						controller.increaseBPM();
					}
				});
				next.setBounds(190, 221, 48, 50);
				panel.add(next);
				
				prev = new JButton("<<");
				prev.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						controller.decreaseBPM();
					}
				});
				prev.setBounds(10, 219, 48, 50);
				panel.add(prev);
			
			
			
			
			
			//Fin Botones y Acciones
			////////////////////////////////////////////////////////////////////////////
			
			//Set window size
			frame.setPreferredSize(new Dimension(700,525));
			 
			// Display the window.
			frame.pack();
			//frame.setLocationByPlatform(true);
			frame.setLocation(180,100);
			frame.setVisible(true);
			
		}
		
		
		
		
		public static void playBombo(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavB.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Dum");
				//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}
		
		public static void playPlatillo(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavP.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Tss");
				//Thread.sleep(1200); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}
		
		public static void playRedoblante(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavR.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Tash");
				//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}

		public void createView() {
			// TODO Auto-generated method stub
			// Create all Swing components here
			/*progressBar_1 = new BeatBar();
			progressBar_1.setBounds(250, 228, 89, 23);
			panel.add(progressBar_1);
			
			progressBar_2 = new BeatBar();
			progressBar_2.setBounds(349, 228, 89, 23);
			panel.add(progressBar_2);
			
			progressBar_3 = new BeatBar();
			progressBar_3.setBounds(446, 228, 89, 23);
			panel.add(progressBar_3);
			*/
	        /* viewPanel = new JPanel(new GridLayout(1, 2));
	        viewFrame = new JFrame("View");
	        viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        viewFrame.setBounds(500, 125, 150, 80);
	        viewFrame.setSize(new Dimension(150, 80));
	        bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER);
			beatBar = new BeatBar();
			beatBar.setValue(0);
	        JPanel bpmPanel = new JPanel(new GridLayout(2, 1));
			bpmPanel.add(beatBar);
	        bpmPanel.add(bpmOutputLabel);
	        viewPanel.add(bpmPanel);
	        viewFrame.getContentPane().add(viewPanel, BorderLayout.CENTER);
	        viewFrame.pack();
	        viewFrame.setVisible(true);
	        viewFrame.setLocation(585, 200);*/ /*****************VIEW POSITION*****/
			
			
		}

		public void createControls() {
			// TODO Auto-generated method stub
			
		}

		public void disableStopMenuItem() {
			// TODO Auto-generated method stub
			btnStop.setEnabled(false);
		}

		public void enableStartMenuItem() {
			// TODO Auto-generated method stub
			btnStart.setEnabled(true);
		}

		public void disableStartMenuItem() {
			// TODO Auto-generated method stub
			btnStart.setEnabled(false);
		}

		public void enableStopMenuItem() {
			// TODO Auto-generated method stub
			btnStop.setEnabled(true);
		}
		
		
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == Set) {
				int bpm = Integer.parseInt(textField.getText());
	        	controller.setBPM(bpm);
			} else if (event.getSource() == next) {
				controller.increaseBPM();
			} else if (event.getSource() == prev) {
				controller.decreaseBPM();
			}
	    }
		
		public void updateBPM() {
			if (model != null) {
				int bpm = model.getBPM();
				if (bpm == 0) {
					if (text != null) {
						text.setText("offline");
					}
				} else {
					if (text != null) {
						text.setText("Instrumento: Bombo, BPM: " + model.getBPM());
					}
				}
			}
		}
	  
		public void updateBeat() {
			if (beatBar1 != null) {
				 beatBar1.setValue(100);
			}
		}
}
